package com.zzzyh.forumsystemclient.cotroller;

import com.alibaba.fastjson.JSON;
import com.zzzyh.forumsystemclient.common.AppResult;
import com.zzzyh.forumsystemclient.model.LoginUser;
import com.zzzyh.forumsystemclient.model.Notifications;
import com.zzzyh.forumsystemclient.model.PageResult;
import com.zzzyh.forumsystemclient.model.UnreadNotificationCountResponse;
import com.zzzyh.forumsystemclient.services.INotificationService;
import com.zzzyh.forumsystemclient.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NotificationsController {
    @Autowired
    private INotificationService notificationService;
    @RequestMapping("/notifications/is-unread")
    public AppResult<Boolean> isUnread(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        Integer userId = user.getUser().getId();
        return AppResult.success(notificationService.isUnread(userId));
    }
    @RequestMapping("/notifications/page")
    public AppResult<PageResult<Notifications>> notifications(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                                              @RequestParam(required = false) String type,
                                                              @RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        Integer userId = user.getUser().getId();
        List<Notifications> notificationList = notificationService.notifications(pageNum, pageSize, type, userId);
        Integer total = notificationService.total(type, userId, null);
        return AppResult.success(new PageResult<>(pageNum, pageSize, total, notificationList));
    }

    @RequestMapping("/notifications/unReadNum")
    public AppResult<UnreadNotificationCountResponse> unReadNum(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        Integer userId = user.getUser().getId();
        UnreadNotificationCountResponse response = new UnreadNotificationCountResponse();
        response.setSystem(new UnreadNotificationCountResponse.NotificationCountDetail(notificationService.total("system", userId,0)));
        response.setComment(new UnreadNotificationCountResponse.NotificationCountDetail(notificationService.total("comment", userId,0)));
        response.setLike(new UnreadNotificationCountResponse.NotificationCountDetail(notificationService.total("like", userId,0)));
        response.setFollow(new UnreadNotificationCountResponse.NotificationCountDetail(notificationService.total("follow", userId,0)));
        return AppResult.success(response);
    }
    @RequestMapping("/notifications/read/{notificationId}")
    public AppResult<String> read( @PathVariable String notificationId) {
        notificationService.read(Integer.valueOf(notificationId));
        return AppResult.success("已读");
    }
    @RequestMapping("/notifications/readAll")
    public AppResult<String> readAll(@RequestHeader("Authorization") String token) {
        LoginUser user = JSON.parseObject(JwtUtils.getSubjectFromToken(token), LoginUser.class);
        Integer userId = user.getUser().getId();
        notificationService.readAll(userId);
        return AppResult.success("全部已读");
    }

    @RequestMapping("/notifications/delete/{notificationId}")
    public AppResult<String> delete( @PathVariable String notificationId) {
        notificationService.delete(Integer.valueOf(notificationId));
        return AppResult.success("删除成功");
    }
}
